// pages/dynamic/index/index.js
const app = getApp()
import {Dynamic} from '../../../model/dynamic'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    ListArr: [],
    openid: '',
    isAnimate:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.statusBarHeight);
    this.getDynamicPage()
    let userinfo = wx.getStorageSync('userInfo')
    if (userinfo) {
      this.setData({
        openid: userinfo.openId
      })
    }

  },
  // 获取分页
  async getDynamicPage() {
    const pageOption = {
      size: 10,
      current: 1
    }
    const res = await Dynamic.getDynamicList(pageOption)
    console.log(res);
      this.setData({
        ListArr: res.data.rows
      })
  },
  // 点赞
  addLike(e) {
    console.log(e);
    let likeNumber = e.currentTarget.dataset.item.like
    let dynamicid = e.currentTarget.dataset.item.dynamic_id
    let data = {
      count: likeNumber + 1,
      dynamicId: dynamicid
    }
    wx.$http.post(wx.$post.postLike, data).then(res => {
      if (res.result.length != 0) {
        this.getDynamicPage()
      }
    })
  },
  goDetail(e) {
    console.log(e);
    let dynamicId = e.currentTarget.dataset.item.dynamic_id
    let comment = e.currentTarget.dataset.item.comment
    wx.navigateTo({
      url: '/pages/dynamic/detail/detail?dynamicid=' + `${dynamicId}&comment=${comment}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDynamicPage()
    this.setData({
      isAnimate:'zoomIn'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('监听页面隐藏');
    this.setData({
      isAnimate:'zoomOut'
    })
    console.log(this.data.isAnimate);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('监听页面卸载');
    this.setData({
      isAnimate:''
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})